//https://leetcode.cn/problems/permutations-ii/
package codeRandomThoughts.Test47全排列II;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution2 {
    List<Integer> path = new ArrayList<>();
    List<List<Integer>> result = new ArrayList<>();
    boolean[] usedY = new boolean[9];

    public List<List<Integer>> permuteUnique(int[] nums) {
        Arrays.sort(nums);
        backtracking(nums);
        return result;
    }


    public void backtracking(int[] nums) {
        if (path.size() == nums.length) {
            result.add(new ArrayList<>(path));
            return;
        }

        boolean[] usedX = new boolean[21];
        for (int i = 0; i < nums.length; i++) {
            //X方向记录的是数字的数值不重复
            if (!usedX[nums[i] + 10]) {
                //Y方向记录的是数字的位置不重复
                if (!usedY[i]) {

                    usedX[nums[i] + 10] = true;
                    path.add(nums[i]);
                    usedY[i] = true;
                    backtracking(nums);
                    path.remove(path.size() - 1);
                    usedY[i] = false;
                }
            }
        }
    }
}
